<template>
  <div class="list">
    <div class="list-box" v-for="(item, index) in list" :key="index">
      <div class="left">
        <!-- 播放图标 -->
        <i @click="getid(item.id, item.name, item)"></i>
        <!-- 图片 -->
        <div class="img">
          <img width="50px" height="50px" :src="item.coverImgUrl" alt="" />
          <!-- 遮罩 -->
          <div class="msk" @click="getid(item.id)"></div>
        </div>
        <!-- 歌单名 -->
        <p @click="getid(item.id)">{{ item.name }}</p>
      </div>
      <div class="right">
        <div class="center" >
          <!-- 歌单多少歌曲 -->
          <span>{{ item.trackCount }}首</span>
          <!-- 歌单作者 -->
          <span>by {{ item.creator.nickname }}</span>
        </div>
        <!-- 收藏数 -->
        <span>收藏</span>
        <span class="bookCount"> {{ item.bookCount | bookcount }}</span>
        <span>收听：{{ item.playCount | count }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'list',
  data() {
    return {
      limit: 30,
      list: '',
      total: 0,
    }
  },
  methods: {
    getid(id) {
        this.$router.push(`/songlist/${id}`)
    },
    async search() {
      const { data: res } = await this.$http.get(
        `/cloudsearch?keywords=${this.$store.state.search.searchName}&type=1000&limit=${this.limit}`
      )
      console.log(res.result)
      if (res.code !== 200) return this.$message.error('搜索失败')
      this.list = res.result.playlists
      // 总数传到vuex
      this.$store.commit('search/getsongCount', res.result.playlistCount)
      // 分页多少条
      this.total = Number((res.result.playlistCount / 30).toFixed(0) + '0')
    },
  },
  created() {
    this.search()
    this.$store.commit('search/getsearchlist', this.$route.meta.title)
  },
  // 收藏 & 收听 过滤器
  filters: {
    bookcount(v) {
      return v > 100000 ? (v / 10000).toFixed(1) + '万' : v
    },
    count(v) {
      return v > 100000 ? parseInt(v / 10000) + '万' : v
    },
  },
}
</script>

<style scoped lang="less">
.list {
  display: flex;
  margin-top: 10px;
  flex-direction: column;
}
.list-box:nth-child(even) {
  background-color: #f7f7f7;
}
.list-box:hover {
  border: 1px solid #cccccc;
  box-sizing: border-box;
  background: #f2f2f2;
}
.list-box {
  border: 1px solid white;
  height: 62px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  // 左边
  .left {
    cursor: pointer;
    display: flex;
    align-items: center;
    .img {
      position: relative;
      .msk {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: url(../../../assets/images/coverall.png) -160px 0;
      }
    }
    p {
      color: #0c73c2;
      font-size: 12px;
      margin-left: 20px;
      overflow: hidden;
      display: block;
      white-space: nowrap;
      text-overflow: ellipsis;
    }
    i {
      z-index: 2;
      width: 17px;
      height: 17px;
      cursor: pointer;
      display: block;
      background: url(../../../assets/images/table.png) 0 -103px;
      margin: 0 20px;
    }
  }
  // 中间
  .right {
    display: flex;
    font-size: 12px;
    color: #999;
    padding: 0 20px;
    .center {
      margin-right: 80px;
      width: 200px;
      overflow: hidden;
      overflow: hidden;
      display: block;
      white-space: nowrap;
      text-overflow: ellipsis;
      span {
        margin-right: 10px;
        color: #666;
      }
    }
    .bookCount {
      margin-right: 25px;
      float: left;
    }
  }
}
</style>